/*
* $Id: PictureButton.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "PictureButton.h"
#include "Common/HLS_RGB.h"

#define COLOR_NONSEL		RGB(248, 210, 122)
#define COLOR_NONSEL_TEXT	RGB(122, 210, 248)

CPictureButton::CPictureButton(int IdResource, const std::tstring& strNameButton, int posText){
	m_pBitmap = NULL;
	m_pBitmapSelected = NULL;
	m_nIdResource = IdResource;
	m_strNameButton = strNameButton;
	m_nPosText = posText;
	m_pFont = NULL;
}

CPictureButton::~CPictureButton(){
	if(m_pFont){
		m_pFont->DeleteObject();
		delete m_pFont;
	}
	if(m_pBitmap){
		m_pBitmap->DeleteObject();
		delete m_pBitmap;
	}
	if(m_pBitmapSelected){
		m_pBitmapSelected->DeleteObject();
		delete m_pBitmapSelected;
	}
}

BOOL CPictureButton::Create(const RECT& rect, CWnd* pParentWnd, UINT nID){
	return __super::Create(m_strNameButton.c_str(), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, rect, pParentWnd, nID);
}

BEGIN_MESSAGE_MAP(CPictureButton, CButton)
	ON_WM_CREATE()
	ON_WM_SETCURSOR()
	ON_WM_SIZE()
END_MESSAGE_MAP()

int CPictureButton::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int nRes = __super::OnCreate(lpCreateStruct);
	m_pFont = CCommonFunction::GetFont(_T("Book Antiqua"), 22, TRUE, TRUE);
	SetFont(m_pFont, TRUE);
	return nRes;
}
void CPictureButton::OnSize(UINT nType, int cx, int cy){
	m_strVisualName.clear();
	__super::OnSize(nType, cx, cy);
}
void CPictureButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct){
	CRect rect(lpDrawItemStruct->rcItem);
	InitFirstDraw(rect, lpDrawItemStruct->hDC);
	COLORREF colorBk = COLOR_NONSEL_TEXT;
	CBitmap* pBitmap = m_pBitmap;
	if(lpDrawItemStruct->itemState & ODS_SELECTED){
		pBitmap = m_pBitmapSelected;
		colorBk = GetSelColor(COLOR_NONSEL_TEXT);
	}
	CBrush hBrush;
	hBrush.CreateSolidBrush(colorBk);
	FillRect(lpDrawItemStruct->hDC, rect, hBrush);
	::DrawState(lpDrawItemStruct->hDC, NULL, NULL, (LPARAM)pBitmap->m_hObject, 0,
					m_rectImage.left, m_rectImage.top, m_rectImage.Size().cx, m_rectImage.Size().cy,
					DST_BITMAP | DSS_NORMAL);
	SetBkColor(lpDrawItemStruct->hDC, colorBk);
	DrawText(lpDrawItemStruct->hDC, m_strVisualName.c_str(), (int)m_strNameButton.size(), m_rectText, DT_CENTER);
}

BOOL CPictureButton::OnSetCursor(CWnd*, UINT, UINT){
	::SetCursor(LoadCursor(NULL, IDC_HAND));
	return TRUE;
}

void CPictureButton::SetTextDraw(HDC dc){
	std::vector<std::tstring> aWord = CStringFunction::GetStringArray(m_strNameButton, _T(" "));
	CSize size = m_rectText.Size();
	SIZE sizeSpace;
	GetTextExtentPoint32(dc, _T(" "), 1, &sizeSpace);
	SIZE sizeWord = {0};
	int nLine = 1;
	int pos = 0;
	for (std::vector<std::tstring>::iterator it = aWord.begin(); it != aWord.end();){
		m_strVisualName.append(*it);
		GetTextExtentPoint32(dc, it->c_str(), (int)it->size(), &sizeWord);
		pos = pos + sizeWord.cx + sizeSpace.cx;
		it++;
		if(it == aWord.end())
			break;
		GetTextExtentPoint32(dc, it->c_str(), (int)it->size(), &sizeWord);
		if(pos + sizeWord.cx > size.cx){
			m_strVisualName.append(_T("\n"));
			pos = 0;
			nLine++;
		}
		else
			m_strVisualName.append(_T(" "));
	}
	int cy = nLine * sizeWord.cy;
	m_rectText.top += (size.cy - cy) / 2;
	m_rectText.bottom -= (size.cy - cy) / 2;
}

CBitmap* CPictureButton::SetBkBitmap(bool bSel){
	CBitmap* bitmap = new CBitmap;
	bitmap->LoadBitmap(m_nIdResource);
	BITMAP bmp;
	memset(&bmp, 0, sizeof(BITMAP));
	bitmap->GetBitmap(&bmp);
	int nNumPixel = bmp.bmHeight * bmp.bmWidth;
	int* buf = new int[nNumPixel];
	bitmap->GetBitmapBits(nNumPixel * sizeof(int), buf);
	for (int i = 0; i < nNumPixel; i++){
		if(!(~buf[i] & 0xffffff))
				buf[i] = COLOR_NONSEL;
		if(bSel)
			buf[i] = GetSelColor(buf[i]);
	}
	bitmap->SetBitmapBits(nNumPixel * sizeof(int), buf);
	delete buf;
	return bitmap;
}
COLORREF CPictureButton::GetSelColor(COLORREF buf){
	COLORREF color = RGB(GetBValue(buf), GetGValue(buf), GetRValue(buf));
	WORD h, l, s;
	RGBtoHLS(color, h, l, s);
	if(l - 50 > 0)
		l -= 50;
	color = HLStoRGB(h, l, s);
	return RGB(GetBValue(color), GetGValue(color), GetRValue(color));
}
void CPictureButton::InitFirstDraw(CRect rectWin, HDC dc){
	if(!m_pBitmap)
		m_pBitmap = SetBkBitmap(false);
	if(!m_pBitmapSelected)
		m_pBitmapSelected = SetBkBitmap(true);
	if(m_strVisualName.empty()){
		BITMAP bmp = {0};
		m_pBitmap->GetBitmap(&bmp);
		if(m_nPosText == RIGHT_POS){
			m_rectImage.SetRect(0, 0, bmp.bmWidth, bmp.bmHeight);
			m_rectText.SetRect(bmp.bmWidth, 0, rectWin.right, rectWin.bottom);
		}else if(m_nPosText == LEFT_POS){
			m_rectImage.SetRect(rectWin.right - bmp.bmWidth, 0, rectWin.right, bmp.bmHeight);
			m_rectText.SetRect(0, 0, rectWin.right, rectWin.bottom);
		}else if(m_nPosText == DOWN_POS){
			int cx = (rectWin.Size().cx - bmp.bmWidth) / 2;
			m_rectImage.SetRect(cx, 0, cx + bmp.bmWidth, bmp.bmHeight);
			m_rectText.SetRect(0, bmp.bmHeight, rectWin.right, rectWin.bottom);
		}else if(m_nPosText == UP_POS){
			int cx = (rectWin.Size().cx - bmp.bmWidth) / 2;
			m_rectImage.SetRect(cx, rectWin.bottom - bmp.bmHeight, cx + bmp.bmWidth, rectWin.bottom);
			m_rectText.SetRect(0, 0, rectWin.right, rectWin.bottom - bmp.bmHeight);
		}
		SetTextDraw(dc);
	}
}
